package com.briup.srms.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

	/**
	 * 将自定义拦截器作为Bean写入配置
	 * @return
	 */
	@Bean
	public JwtInterceptor jwtInterceptor() {
		return new JwtInterceptor();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//拦截路径可自行配置多个 可用 ，分隔开
		registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**")
				.excludePathPatterns(

				        // 权限有问题，临时取消拦截
						"/**",
                        "/exam/paper/导出word",
						"/file/upload",
						"/device/download",
						"/baseUser/register",
						"/student/register",
						"/swagger-resources/**",
                        "/v2/**",
                        "/swagger-ui.html",
                        "/webjars/**",
						"/user/login",
						"/user/info",
                        "/user/logout",
                        "/article/download",
                        "/privilege/findPrivilegeTree"
                );
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH")
				.allowedHeaders("*")
				.allowCredentials(true)
				.maxAge(3600);
	}

}
